// eslint.config.js
import { dirname } from "path";
import { fileURLToPath } from "url";
import { FlatCompat } from "@eslint/eslintrc";
import typescriptEslint from "@typescript-eslint/eslint-plugin";
import tsParser from "@typescript-eslint/parser";
import prettierConfig from "eslint-config-prettier";
import unusedImports from "eslint-plugin-unused-imports"; // 新增

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// 兼容旧的 extends 写法
const compat = new FlatCompat({
  baseDirectory: __dirname,
});

export default [
  // 1️⃣ Next.js 官方规则（含 React、JS/TS 最佳实践）
  ...compat.extends("next/core-web-vitals", "next/typescript"),

  // 2️⃣ 启用 TypeScript ESLint 插件
  {
    files: ["**/*.{ts,tsx}"],
    languageOptions: {
      parser: tsParser,
    },
    plugins: {
      "@typescript-eslint": typescriptEslint,
      "unused-imports": unusedImports, // 新增
    },
    rules: {
      // 删除未使用的 import
      "unused-imports/no-unused-imports": "error",
      "unused-imports/no-unused-vars": "error",// 增强对导入变量的检测
      // 未使用变量报错，--fix不会自动删除，需手动删除，默认不支持自动修复（fixable: false）
      "@typescript-eslint/no-unused-vars": [
        "error",
        {
          argsIgnorePattern: "^_",
          varsIgnorePattern: "^_",
          caughtErrorsIgnorePattern: "^_",
        },
      ],
    },
  },

  // 3️⃣ JS 文件的未使用变量规则（避免和 TS 重复）
  {
    files: ["**/*.{js,jsx}"],
    plugins: {
      "unused-imports": unusedImports, // 新增
    },
    rules: {
      "unused-imports/no-unused-imports": "error",
      "no-unused-vars": [
        "error",
        {
          argsIgnorePattern: "^_",
          varsIgnorePattern: "^_",
        },
      ],
    },
  },

  // 4️⃣ Prettier 关闭 ESLint 冲突规则
  prettierConfig,
];
